/*
package csu.web.mypetstore.web.servlet;

import csu.web.mypetstore.domain.Account;
import csu.web.mypetstore.domain.Product;
import csu.web.mypetstore.persistence.AccountDao;
import csu.web.mypetstore.persistence.impl.AccountDaoImpl;
import csu.web.mypetstore.service.AccountService;
import csu.web.mypetstore.service.CatalogService;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.List;

public class SignOnServlet extends HttpServlet {

    private static final String SIGN_ON_FORM = "/WEB-INF/jsp/account/signon.jsp";

    private String username;
    private String password;
    private String msg;

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.username = req.getParameter("username");
        this.password = req.getParameter("password");

        //校验用户输入正确性
        if(!validate()){
            req.setAttribute("signOnMsg", this.msg);
            req.getRequestDispatcher(SIGN_ON_FORM).forward(req, resp);
        }
        else {
            AccountService accountService=new AccountService();
            Account loginAccount=accountService.getAccount(username, password);

            loginAccount.setUsername(username);
            loginAccount.setPassword(password);

            AccountDaoImpl accountDao=new AccountDaoImpl();
            boolean test=accountDao.selectAccountByUsername(loginAccount.getUsername());
            boolean test1=accountDao.selectAccountByPassword(loginAccount.getPassword());

            System.out.println("help");

            if(test && test1){

                loginAccount.setPassword(null);
                HttpSession session=req.getSession();
                session.setAttribute("loginAccount", loginAccount);

                if(loginAccount.isListOption()){
                    CatalogService catalogService=new CatalogService();
                    List<Product> myList=catalogService.getProductListByCategory(loginAccount.getFavouriteCategoryId());
                    session.setAttribute("myList", myList);
                }
                resp.sendRedirect("mainForm");
            }
            else {
                this.msg="用户名或密码错误";
                req.setAttribute("signOnMsg", this.msg);
                req.getRequestDispatcher(SIGN_ON_FORM).forward(req, resp);
            }
        }
    }

    public boolean validate(){
        if(this.username.equals("") || this.username==null){
            this.msg = "用户名为空";
            return false;
        }
        if(this.password.equals("") || this.password==null){
            this.msg = "密码为空";
            return false;
        }
        if(this.username.length()>8){
            this.msg = "用户名过长";
            return false;
        }
        if(this.password.length()>8){
            this.msg = "密码过长";
            return false;
        }
        return true;
    }
}*/
package csu.web.mypetstore.web.servlet;

import csu.web.mypetstore.domain.Account;
import csu.web.mypetstore.domain.Product;
import csu.web.mypetstore.persistence.AccountDao;
import csu.web.mypetstore.persistence.impl.AccountDaoImpl;
import csu.web.mypetstore.service.AccountService;
import csu.web.mypetstore.service.CatalogService;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.List;
import com.google.gson.Gson;
import static com.google.code.kaptcha.Constants.KAPTCHA_SESSION_KEY;

public class SignOnServlet extends HttpServlet {

    private static final String SIGN_ON_FORM = "/WEB-INF/jsp/account/signon.jsp";

    private String username;
    private String password;
    private String msg;
    String token;//正确的验证码
    String code;//用户输入的验证码


    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.username = req.getParameter("username");
        this.password = req.getParameter("password");

        HttpSession session = req.getSession();

        //获取验证码
        token = (String) session.getAttribute(KAPTCHA_SESSION_KEY);
        //删除验证码
        session.removeAttribute(KAPTCHA_SESSION_KEY);
        //获取用户输入的验证码
        code = req.getParameter("code");

        //校验用户输入正确性
        if(!validate()){
            req.setAttribute("signOnMsg", this.msg);
            req.getRequestDispatcher(SIGN_ON_FORM).forward(req, resp);
        } else if (token != null && token.equals(code)) {
            AccountService accountService=new AccountService();
            Account loginAccount=accountService.getAccount(username, password);

            loginAccount.setUsername(username);
            loginAccount.setPassword(password);

            AccountDaoImpl accountDao=new AccountDaoImpl();
            boolean test=accountDao.selectAccountByUsername(loginAccount.getUsername());
            boolean test1=accountDao.selectAccountByPassword(loginAccount.getPassword());

            System.out.println("help");

            if(test && test1){

                loginAccount.setPassword(null);
                //HttpSession session=req.getSession();
                session.setAttribute("loginAccount", loginAccount);

                if(loginAccount.isListOption()){
                    CatalogService catalogService=new CatalogService();
                    List<Product> myList=catalogService.getProductListByCategory(loginAccount.getFavouriteCategoryId());
                    session.setAttribute("myList", myList);
                }
                resp.sendRedirect("mainForm");
            }
            else {
                this.msg="用户名或密码错误";
                req.setAttribute("signOnMsg", this.msg);
                req.getRequestDispatcher(SIGN_ON_FORM).forward(req, resp);
            }
        } else {
            this.msg="验证码错误";
            req.setAttribute("signOnMsg", this.msg);
            req.getRequestDispatcher(SIGN_ON_FORM).forward(req, resp);
        }
    }

    public boolean validate(){
        if(this.username.equals("") || this.username==null){
            this.msg = "用户名为空";
            return false;
        }
        if(this.password.equals("") || this.password==null){
            this.msg = "密码为空";
            return false;
        }
        if(this.username.length()>8){
            this.msg = "用户名过长";
            return false;
        }
        if(this.password.length()>8){
            this.msg = "密码过长";
            return false;
        }
        return true;
    }
}